package com.lemon.stock.compose

import android.content.Intent
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.lemon.stock.theme.*
import com.lemon.stock.ui.ConfigureActivity
import com.lemon.stock.ui.HistoryActivity
import com.lemon.stock.util.showMessage
import com.lemon.stock.viewmodel.StocksViewModel

/**
 * 概况
 */
@Composable
fun SurveyScreen(){
    Column(
        modifier = Modifier.fillMaxSize().padding(horizontal = 8.dp),
        //verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        val mViewModel: StocksViewModel = viewModel()
        val stocks = mViewModel.stocks.observeAsState(arrayListOf())
        mViewModel.fetchStocks()
        var liSurvey = mapOf<Int, SurveyItem>(
            SurveyType.amount to SurveyItem(title="股票数", display = "int"),
            SurveyType.float to SurveyItem(title="浮动收益"),
            SurveyType.cost to SurveyItem(title="股票成本"),
            SurveyType.bonus to SurveyItem(title="分红"),
            SurveyType.commission to SurveyItem(title="手续费"),
            SurveyType.transfer to SurveyItem(title="过户费"),
            SurveyType.tax to SurveyItem(title="印花税"),
            SurveyType.taxb to SurveyItem(title="红利税"),
            SurveyType.history to SurveyItem(title="历史收益"),
        )
        var total: Float = 0f

        stocks.value.forEach(){stock ->
            if(stock.holdStock>0){
                liSurvey[SurveyType.amount]!!.figure ++
            }
            total += stock.worthMoney
            liSurvey[SurveyType.history]!!.figure += stock.totalProfit
            liSurvey[SurveyType.float]!!.figure += stock.profitMoney
            liSurvey[SurveyType.cost]!!.figure += stock.investMoney
            liSurvey[SurveyType.bonus]!!.figure += stock.bonusMoney
            liSurvey[SurveyType.commission]!!.figure += stock.commissionMoney
            liSurvey[SurveyType.transfer]!!.figure += stock.transferMoney
            liSurvey[SurveyType.tax]!!.figure += stock.taxMoney
            liSurvey[SurveyType.taxb]!!.figure += stock.bonusTax
        }
        if(liSurvey[SurveyType.float]!!.figure>0){
            liSurvey[SurveyType.float]!!.style = styleEarn
        }else if(liSurvey[SurveyType.float]!!.figure<0){
            liSurvey[SurveyType.float]!!.style = styleLose
        }

        surveyHead(total)

        surveyList(liSurvey.values.toList())
    }
}
@Composable
fun surveyHead(total: Float){
    val context = LocalContext.current
    Spacer(modifier = Modifier.height(15.dp))
    Box(
        modifier = Modifier
            .width(120.dp).height(120.dp)
            .background(
                color = red500,
                shape = RoundedCornerShape(60.dp)
            )
            .pointerInput(Unit) {
                detectTapGestures(
                    onLongPress = {
                        context.startActivity(Intent(context, HistoryActivity::class.java))
                        showMessage(context, "activity test")
                    }
                )
            }
    ){
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.align(Alignment.Center)
                //.clickable {}

        ) {
            Text(text = "总市值", style= TextStyle(color = Color.LightGray, fontSize = 18.sp), modifier = Modifier.padding(vertical = 10.dp))
            Text(text = String.format("%,.2f", total), color=Color.White, fontSize = 16.sp)
        }
    }
    Spacer(modifier = Modifier.height(15.dp))
}

@Composable
fun surveyList(surveys:List<SurveyItem>){
    Divider(color = back002, thickness = 0.5.dp)
    LazyColumn(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
            .background(color = Color.White)
    ) {
        items(surveys){ survey->
            Row(
                modifier = Modifier.height(35.dp)
            ) {
                val display = when(survey.display){
                    "money" -> String.format("%,.2f", survey.figure)
                    "int" -> survey.figure.toInt().toString()
                    else -> survey.figure.toString()
                }
                Text(text = survey.title, style= styleCaption, modifier=Modifier.weight(1f).align(Alignment.CenterVertically))
                Text(text = display, style= survey.style, modifier = Modifier.align(Alignment.CenterVertically))
            }
            Divider(color = back002, thickness = 0.5.dp)
        }
    }

}

class SurveyItem(
    var title: String = "天道酬勤",
    var figure: Float = 0f,
    var style: TextStyle = styleBodyRight,
    var display: String = "money"
)

object SurveyType{
    const val amount = 1
    const val history = 2
    const val float = 3
    const val cost = 4
    const val bonus = 5
    const val commission = 6
    const val transfer = 7
    const val tax = 8
    const val taxb = 9
}